package com.demo.burt.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.demo.burt.dataobject.TestUser;
import com.demo.burt.mysql.tech.read.dao.TestUserReadMapper;
import com.demo.burt.mysql.tech.write.dao.TestUserMapper;
import com.demo.burt.service.TestUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 用户服务实现类
 * </p>
 *
 * @author luowei1
 * @since 2018-12-12
 */
@Service
@Slf4j
public class TestUserServiceImpl extends ServiceImpl<TestUserMapper, TestUser> implements TestUserService {

    @Autowired
    private TestUserMapper     testUserWriterMapper;
    @Autowired
    private TestUserReadMapper testUserReadMapper;

    /**
     * <p>
     * 根据id查询{@link TestUser}
     * </p>
     *
     * @param userId 用户id
     * @return 返回<code>TestUser</code>，数据不存在，返回<code>null</code>
     */
    @Override
    public TestUser getUserById(Long userId) {
        //        SearchShopOperationIntegralRequest requestSearch = new SearchShopOperationIntegralRequest();
        //        requestSearch.setShopID(38);
        //        requestSearch.setStartDate(DateUtils.toCalendar(new Date()));
        //        requestSearch.setEndDate(DateUtils.toCalendar(new Date()));
        //        requestSearch.setPageIndex(1);
        //        requestSearch.setPageSize(100);
        //        requestSearch.setStatus(1);
        //        try {
        //            SearchShopOperationIntegralResponse result = shopOperationWSClient
        //                    .searchShopOperationIntegral(requestSearch);
        //            log.info("wcf请求结果:{}", result);
        //        } catch (Exception e) {
        //            e.printStackTrace();
        //        }
        //        log.error("error级别日志测试");
        //        testUserReadMapper.selectOne(null);
        return testUserReadMapper.selectById(userId);
    }

    @Override
    public TestUser getUserByIdW(Long userId) {
        return testUserWriterMapper.selectById(userId);
    }

    /**
     * <p>
     * 根据id批量查询{@link TestUser}
     * </p>
     *
     * @param userIds 用户id
     * @return 返回<code>List<TestUser></Test></code>，数据不存在，返回<code>Empty</code>
     */
    @Override
    public List<TestUser> getUsersByIds(List<Long> userIds) {
        //相当于调用testUserWriterMapper的getById方法
        return selectBatchIds(userIds);
    }

    /**
     * <p>
     * 根据条件查询{@link TestUser}
     * </p>
     *
     * @param map 查询条件
     * @return 返回<code>List<TestUser></code>，数据不存在，返回<code>Empty</code>
     */
    @Override
    public List<TestUser> selectTestByMap(Map<String, Object> map) {
        //相当于调用testUserReadMapper的selectByMap方法
        return testUserReadMapper.selectByMap(map);
    }

    /**
     * <p>
     * 根据手机号查询{@link TestUser}
     * </p>
     *
     * @param mobile 手机号
     * @return 返回<code>TestUser</code>，数据不存在，返回<code>null</code>
     */
    @Override
    public TestUser selectOneUser(String mobile) {
        Wrapper<TestUser> wrapper = new EntityWrapper<>();
        wrapper.eq("mobile", mobile);
        return selectOne(wrapper);
    }

    /**
     * <p>
     * 根据手机号查询{@link TestUser}
     * </p>
     *
     * @param mobile 手机号
     * @return 返回<code>TestUser</code>，数据不存在，返回<code>null</code>
     */
    @Override
    public TestUser selectOneUserRdb(String mobile) {
        Wrapper<TestUser> wrapper = new EntityWrapper<>();
        wrapper.eq("mobile", mobile);
        TestUser user = new TestUser();
        user.setMobile(mobile);
        return testUserReadMapper.selectOne(user);
    }

    /**
     * <p>
     * 根据条件查询{@link TestUser}，返回map集合
     * </p>
     *
     * @param mobile 手机号
     * @return 返回<code>Map<String, Object></code>，数据不存在，返回<code>null</code>
     */
    @Override
    public List<Map<String, Object>> selectMapUser(String mobile) {
        Wrapper<TestUser> wrapper = new EntityWrapper<>();
        wrapper.eq("mobile", mobile);
        return testUserReadMapper.selectMaps(wrapper);
    }

    /**
     * <p>
     * 根据条件查询{@link TestUser}的数量
     * </p>
     *
     * @param userName 用户名
     * @return 返回符合条件的数量，数据不存在，返回<code>0</code>
     */
    @Override
    public Integer selectCountUser(String userName) {
        Wrapper<TestUser> wrapper = new EntityWrapper<>();
        wrapper.eq("user_name", userName);
        return testUserReadMapper.selectCount(wrapper);
    }

    /**
     * <p>
     * 根据条件查询{@link TestUser}
     * </p>
     *
     * @param userName 用户名
     * @return 返回<code>List<TestUser></code>，数据不存在，返回<code>Empty</code>
     */
    @Override
    public List<TestUser> queryUsers(String userName) {
        Wrapper<TestUser> wrapper = new EntityWrapper<>();
        wrapper.eq("user_name", userName);
        return testUserReadMapper.selectList(wrapper);
    }

    /**
     * <p>
     * 查询在某个时间节点(含)之前注册的用户(<=){@link TestUser}
     * </p>
     *
     * @param joinDate 时间节点
     * @param pageNum 分页页码 从1开始
     * @param pageSize 每页大小
     * @return 返回<code>PageInfo<TestUser></code>
     */
    @Override
    public PageInfo<TestUser> queryUserPage(Date joinDate, int pageNum, int pageSize) {
        //分页推荐使用com.github.pagehelper.PageHelper
        PageHelper.startPage(pageNum, pageSize);
        Wrapper<TestUser> wrapper = new EntityWrapper<>();
        //查询在joinDate前注册的用户(<=)
        //        wrapper.le("created_time", joinDate);
        List<TestUser> users = testUserReadMapper.selectList(wrapper);
        //        Pagination pagination = new Pagination(pageNum, pageSize);
        //        List<TestUser> users = testUserReadMapper.selectPage(pagination, wrapper);
        return new PageInfo<>(users);
    }

    /**
     * <p>
     * 根据用户名查找用户
     * </p>
     *
     * @param userName 用户名
     * @return 返回<code>TestUser</code>，数据不存在，返回<code>null</code>
     */
    @Override
    public TestUser getTestByName(String userName) {
        Wrapper<TestUser> wrapper = new EntityWrapper<>();
        wrapper.eq("user_name", userName);
        List<TestUser> users = testUserReadMapper.selectList(wrapper);
        return null == users || users.isEmpty() ? null : users.get(0);
    }

    @Override
    public List<TestUser> getAllUser() {
        Wrapper<TestUser> wrapper = new EntityWrapper<>();
        //        wrapper.eq("is_deleted", 0);
        return testUserReadMapper.selectList(wrapper);
    }

}
